package net.alteiar.ui.algorithms;

import java.awt.Color;

import net.alteiar.algorithms.ClusteringUtils;
import net.alteiar.algorithms.ImageInfoUtils;
import net.alteiar.algorithms.builder.RGBAverageBuilder;
import net.alteiar.images.BasicImage;
import net.alteiar.images.IDrawable;
import net.alteiar.images.ImageFiltered;
import net.alteiar.images.filter.ClusterFilter;
import net.alteiar.images.metadata.ImageInfo;

public class AlgorithmClustering implements Algorithm {

	public static String NAME = "K-Means";

	private int clusterCount;
	private int precision;
	private Color[] colors;

	public AlgorithmClustering() {
		clusterCount = 5;
		precision = 5;
		colors = null;
	}

	public void setColors(Color[] colors) {
		this.colors = colors;
	}

	public Color[] getColor() {
		return this.colors;
	}

	public void setClusterCount(int precision) {
		this.clusterCount = precision;
	}

	public int getClusterCount() {
		return this.clusterCount;
	}

	public void setPrecision(int precision) {
		this.precision = precision;
	}

	public int getPrecision() {
		return precision;
	}

	@Override
	public IDrawable execute(BasicImage img) {
		ImageInfo info = ImageInfoUtils.buildAverage(img, precision);
		ImageInfo clusters = ClusteringUtils.createKMeansGraphics(info,
				new RGBAverageBuilder(), clusterCount);

		ClusterFilter filter = new ClusterFilter(clusters, colors);

		ImageFiltered filtered = new ImageFiltered(img);
		filtered.addImage(filter);

		return filtered;
	}

	@Override
	public String toString() {
		return NAME;
	}

}
